
'use strict';

const {
  SUCCESS,
  ERROR,
} = require('./responseCode');

module.exports = class ServerResponse {
  constructor(status, msg, data) {
    if (status === 500) {
      this.error = status;
    } else {
      this.success = status;
    }
    this.message = msg;
    this.data = data;
  }

  isSuccess() {
    return this.success === SUCCESS;
  }

  getStatus() {
    return this.success || this.error;
  }

  getData() {
    return this.data;
  }

  getMsg() {
    return this.msg;
  }

  static createBySuccess() {
    return new ServerResponse(SUCCESS);
  }

  static createBySuccessMsg(msg) {
    return new ServerResponse(SUCCESS, msg, null);
  }

  static createBySuccessData(data) {
    return new ServerResponse(SUCCESS, null, data);
  }

  static createBySuccessMsgAndData(msg, data) {
    return new ServerResponse(SUCCESS, msg, data);
  }

  static createByError() {
    return new ServerResponse(ERROR, 'error', null);
  }

  static createByErrorMsg(errorMsg) {
    return new ServerResponse(ERROR, errorMsg, null);
  }

  static createByErrorCodeMsg(errorCode, errorMsg) {
    return new ServerResponse(errorCode, errorMsg, null);
  }
  static logError(error) {
    return new ServerResponse(500, 'error', error);
  }
};
